<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 200px;
            height: 200px;
            border: 2px solid black;
            position: absolute;
            top: 30%;
            left: 45%;
            text-align: center;
            display: none;
            /* position: relative; */
        }

        .shut {
            position: absolute;
            top: 26%;
            left: 55%;
            display: none;
        }

        .ad {

            width: 200px;
            height: 175px;
            background-color: red;
            text-align: center;
            line-height: 175px;

        }
    </style>
</head>

<body>

    <div class="box">

        <div class="ad">这是小广告</div>
        <p>距离结束还有<span>10</span>秒</p>
    </div>
    <button class="shut">关闭</button>

    <script>

        Adv();
        function Adv() {
            setTimeout(function () {
                // 延迟结束后内容显示
                document.querySelector(".box").style.display = "block";
                document.querySelector(".shut").style.display = "block";

                var adv = setInterval(function () {
                    // 每过一秒秒数就减一
                    document.querySelector(".box p span").innerHTML--;
                    if (document.querySelector(".box p span").innerHTML < 0) {
                        // 当秒数小于0时，内容不可见；且需要清除定时器
                        document.querySelector(".box").style.display = "none";
                        document.querySelector(".shut").style.display = "none";
                        clearInterval(adv);

                        // 再过延迟后再执行函数，形成了流氓广告
                        document.querySelector(".box p span").innerHTML = 10;
                        Adv();

                    }
                }, 1000)

                document.querySelector(".shut").addEventListener("click", function () {
                    // 点击时，内容消失
                    document.querySelector(".box").style.display = "none";
                    document.querySelector(".shut").style.display = "none";
                    clearInterval(adv);

                    // 同上
                    document.querySelector(".box p span").innerHTML = 10;
                    Adv();
                })
            }, 3000)
        }





    </script>
</body>

</html>